package com.ceo.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ceo.auth.annotaion.Clear;
import com.ceo.auth.dto.AucLoginDTO;
import com.ceo.auth.entity.AucUser;
import com.ceo.auth.service.AucUserService;
import com.ceo.common.http.response.DefaultResponse;
import com.ceo.common.query.QueryParameter;
import com.ceo.common.threadlocal.GlobalMemcache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.ceo.common.controller.BaseController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author ceo
 * @since 2023-06-02
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/auth/user")
public class AucUserController {

    @Resource
    AucUserService aucUserService;

    @ApiOperation("登录方法")
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public DefaultResponse<String> login(@RequestHeader("User-Agent") String userAgent, @Valid @RequestBody AucLoginDTO aucLoginDTO) {
        return DefaultResponse.success(aucUserService.login(userAgent, aucLoginDTO));
    }

    @Clear
    @ApiOperation("新增用户")
    @RequestMapping(value = "registeUser", method = RequestMethod.POST)
    public DefaultResponse<Boolean> registeUser(@Valid @RequestBody AucUser aucUser) {
        if (aucUserService.countNotDelUser(aucUser.getUserId()) > 0) {
            return DefaultResponse.error(11010003, "用户注册重复");
        }
        return DefaultResponse.success(aucUserService.addUser(aucUser));
    }


    @ApiOperation("获取用户分页列表")
    @RequestMapping(value = "queryPage", method = RequestMethod.POST)
    public DefaultResponse<Page<AucUser>> queryUserPage(@RequestBody QueryParameter<AucUser> qp) {
        QueryWrapper<AucUser> wrapper = qp.createWrapper();
        if (StringUtils.isEmpty((CharSequence) qp.get_params().get("state"))){
            wrapper.and(wa -> wa.eq("state", 0).or().eq("state", 1 ));
        }
        Page<AucUser> res = aucUserService.page(qp.getPage(), wrapper);
        return DefaultResponse.success(res);
    }

    @ApiOperation("获取用户分页列表(通过sql)")
    @RequestMapping(value = "queryUserPageWithSql", method = RequestMethod.POST)
    public DefaultResponse<Page<AucUser>> queryUserPageWithSql(@RequestBody QueryParameter<AucUser> qp) {
        Page<AucUser> res = aucUserService.queryUserPageWithSql( qp);
        return DefaultResponse.success(res);
    }
}

